package com.caocaog.market.front;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caocaog.market.annotation.IgnoreAuth;
import com.caocaog.market.entity.Information;
import com.caocaog.market.entity.InformationReply;
import com.caocaog.market.service.InformationReplyService;
import com.caocaog.market.service.InformationService;
import com.caocaog.market.vo.InformationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.GenericController;
import org.jeecg.common.system.domain.QueryRequest;
import org.jeecg.common.system.query.PageGenerator;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequiredArgsConstructor
@Api(value = "information", tags = InformationFrontController.TABLE_MEANING)
@RequestMapping("/api/information")
public class InformationFrontController extends GenericController<Information, InformationService> {

    public static final String TABLE_MEANING = "信息发布移动端API";
    private final InformationReplyService informationReplyService;

    private static final String ALL_APPROVED = "信息发布移动端API-获取状态为已通过的信息";
    @IgnoreAuth
    @AutoLog(value = ALL_APPROVED)
    @ApiOperation(value = ALL_APPROVED, notes = "只查审核通过的，分站id(siteId)不能为空")
    @GetMapping(value = "/allApprovedList")
    public Result<?> queryInformationPageAllApproved(Information information, QueryRequest queryRequest) {
        Page<InformationVO> page = PageGenerator.createPageSort(queryRequest);
        IPage<InformationVO> allApprovedList = super.service.queryInformationPageAllApproved(page, information);
        return Result.OK(allApprovedList);
    }

    private static final String ALL_YOUR_SELF = "信息发布移动端API-获取自己的信息";
    @IgnoreAuth
    @ApiOperation(value = ALL_YOUR_SELF, notes = "创建人(createBy)不能为空")
    @GetMapping(value = "/allYourSelfList")
    public Result<?> queryInformationPageAllYourSelf(Information information, QueryRequest queryRequest) {
        Page<InformationVO> page = PageGenerator.createPageSort(queryRequest);
        IPage<InformationVO> allApprovedList = super.service.queryInformationPageAllYourSelf(page, information);
        return Result.OK(allApprovedList);
    }

    private static final String ADD_REVIEW = "信息发布移动端API-添加到待审核";
    @IgnoreAuth
    @AutoLog(value = ADD_REVIEW)
    @ApiOperation(value = ADD_REVIEW, notes = ADD_REVIEW)
    @PostMapping(value = "/addReview")
    public Result<?> addReview(@RequestBody InformationVO informationVO) {
        return Result.OK(ADD_SUCCESS, super.service.addInformation(informationVO));
    }

    private static final String EDIT = "信息发布-编辑";
    @IgnoreAuth
    @AutoLog(value = EDIT)
    @ApiOperation(value = EDIT, notes = EDIT)
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody InformationVO informationVO) {
        return Result.OK(EDIT_SUCCESS, super.service.updateInformation(informationVO));
    }

    private static final String EDIT_AND_SUBMIT_REVIEW = "信息发布-编辑AND提交审核";
    @IgnoreAuth
    @AutoLog(value = EDIT_AND_SUBMIT_REVIEW)
    @ApiOperation(value = EDIT_AND_SUBMIT_REVIEW, notes = "编辑之后直接提交审核")
    @PutMapping(value = "/editAndSubmitReview")
    public Result<?> updateAndSubmitReview(@RequestBody InformationVO informationVO) {
        return Result.OK("编辑成功，已提交审核", super.service.updateAndSubmitReviewInformation(informationVO));
    }

    private static final String SUBMIT_REVIEW = "信息发布-提交审核";
    @IgnoreAuth
    @AutoLog(value = SUBMIT_REVIEW)
    @ApiOperation(value = SUBMIT_REVIEW, notes = SUBMIT_REVIEW)
    @PutMapping(value = "/submitReview")
    public Result<?> submitReview(@RequestParam String id) {
        return Result.OK("提交审核成功", super.service.submitReview(id));
    }

    private static final String PUT_ON_SALE = "信息发布-上架";
    @IgnoreAuth
    @AutoLog(value = PUT_ON_SALE)
    @ApiOperation(value = PUT_ON_SALE, notes = PUT_ON_SALE)
    @PutMapping(value = "/putOnSale")
    public Result<?> putOnSaleInfo(@RequestBody Information information) {
        return Result.OK("上架信息发布成功", super.service.putOnSaleInfo(information.getId()));
    }

    private static final String PULL_OFF_SHELVES = "信息发布-下架";
    @IgnoreAuth
    @AutoLog(value = PULL_OFF_SHELVES)
    @ApiOperation(value = PULL_OFF_SHELVES, notes = PULL_OFF_SHELVES)
    @PutMapping(value = "/pullOffShelves")
    public Result<?> pullOffShelvesInfo(@RequestBody Information information) {
        return Result.OK("下架信息发布成功", super.service.pullOffShelvesInfo(information.getId()));
    }

    private static final String GET_BY_ID = "根据id获取信息详细内容";
    @IgnoreAuth
    @AutoLog(value = GET_BY_ID)
    @ApiOperation(value = GET_BY_ID, notes = GET_BY_ID)
    @GetMapping(value = "/detail")
    public Result<?> getDetailById(String id) {
        return Result.OK(super.service.getDetailById(id));
    }

    private static final String PAGE = "内容回复-分页列表查询";
    @IgnoreAuth
    @ApiOperation(value = PAGE, notes = PAGE)
    @GetMapping(value = "/replyList")
    public Result<?> queryPageList(InformationReply informationReply, QueryRequest queryRequest) {
        Page<InformationReply> page = PageGenerator.createPageSort(queryRequest);
        IPage<InformationReply> pageList = this.informationReplyService.queryInformationReplyPage(page, informationReply);
        return Result.OK(pageList);
    }

    private static final String ADD = "内容回复-添加";
    @IgnoreAuth
    @AutoLog(value = ADD)
    @ApiOperation(value = ADD, notes = ADD)
    @PostMapping(value = "/addReply")
    public Result<?> add(@RequestBody InformationReply informationReply) {
        return Result.OK(ADD_SUCCESS, this.informationReplyService.addInformationReply(informationReply));
    }

}
